// pages/search/search.js
const common = require('../../mixins/common')
const mixins = require('../../mixins/forum')
const article = require('../../mixins/article')
const shop = require('../../mixins/shop')
const app = getApp()
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    maxLen: 1000,
    cats: [],
    page: 1,
    shoppage:1,
    searchViewVisible: true,
    tabs: [{
        title: "动态",
        tabIndex: 0
      }, {
        title: "资讯",
        tabIndex: 1
      }, {
        title: "圈子",
        tabIndex: 2
      },
      {
        title: "用户",
        tabIndex: 3
      },
      {
        title: "商品",
        tabIndex: 4
      },
    ],
    topicload:true,
    isLastPage: false,
    currentData: 0,
    posts: [],
    userList: [],
    tags: [],
    products:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTagList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   *  监听输入关键词
   */

  onSearchInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  
  /**
   *  切换菜单
   */

  checkCurrent: function (t) {
    if (this.data.currentData === t.target.dataset.index) return !1;
    this.setData({
      currentData: t.target.dataset.index,
      isLastPage: false,
      page: 1,
      posts: [],
      cats: [],
      userList:[],
    })
    if (t.target.dataset.index == 0) {
      this.getTopicLists({
        search: this.data.keyword,
        page: this.data.page
      })
    } else if (t.target.dataset.index == 1) {
      this.getPosts({
        search: this.data.keyword,
        page: this.data.page
      })
    } else if (t.target.dataset.index == 2) {
      this.getCircleLists({
        search: this.data.keyword,
        page: this.data.page
      })
    } else if (t.target.dataset.index == 3) {
      this.getUserBySearch({
        keyword: this.data.keyword,
        page: this.data.page
      })
    }
    else if (t.target.dataset.index == 4) {
      this.getShopLists({
        keyword: this.data.keyword,
        page: this.data.shoppage
      })
    }
  },
  
  /**
   *  清除搜索
   */
  onClearBtnClick:function(){
   this.setData({
    keyword:'',
   })
  },


  /**
   *  跳转详情页
   */

  gotoDetail: function (e) {
    console.log(e);
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id + '&type=forums',
    })
  },

  /**
   * 确定搜索
   */
  onSearchConfirm: function () {
    this.setData({
      isLastPage: false,
      page: 1,
      posts: [],
      cats: [],
      userList:[],
    })
    if (this.data.currentData == 0) {
      this.getTopicLists({
        search: this.data.keyword,
        page: this.data.page
      })
    } else if (this.data.currentData == 1) {
      this.getPosts({
        search: this.data.keyword,
        page: this.data.page
      })
    } else if (this.data.currentData == 2) {
      this.getCircleLists({
        search: this.data.keyword,
        page: this.data.page
      })
    } else if (this.data.currentData == 3) {
      this.getUserBySearch({
        keyword: this.data.keyword,
        page: this.data.page
      })
    }
    else if (this.data.currentData == 4) {
      this.getShopLists({
        keyword: this.data.keyword,
        page: this.data.shoppage
      })
    }
  },



  searchStat: function (e) {
    let keyword = e.currentTarget.dataset.name;
    this.setData({
      keyword: keyword
    })
    this.onSearchConfirm();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let user = app.globalData.user
    if (!user) {
      user = '';
    }
    this.setData({
      user: user,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isBottom: true,
    })
    if (!this.data.isLastPage) {
      if (this.data.currentData == 0) {
        this.getTopicLists({
          search: this.data.keyword,
          page: this.data.page
        })
      } else if (this.data.currentData == 1) {
        this.getPosts({
          search: this.data.keyword,
          page: this.data.page
        })
      } else if (this.data.currentData == 2) {
        this.getCircleLists({
          search: this.data.keyword,
          page: this.data.page
        })
      } else if (this.data.currentData == 3) {
        this.getUserBySearch({
          search: this.data.keyword,
          page: this.data.page
        })
      }
      else if (this.data.currentData  == 4) {
        this.getShopLists({
          keyword: this.data.keyword,
          page: this.data.shoppage
        })
      }
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}

common(options)
mixins(options)
article(options)
shop(options)
Page(options)